package com.sk.cardeal.act.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.ProjectAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.dialog.BuyNextDialogFragment;
import com.sk.cardeal.dialog.ProjectNextDialogFragment;
import com.sk.cardeal.entity.Project;
import com.sk.cardeal.entity.ProjectListBean;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.SnackUtils;
import com.sk.cardeal.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by sk on 2017/12/25.
 */

public class ChooseFixProjectActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener, HandleDataCallBack {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.apply_form)
    TextView applyForm;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;

    private int type;

    private ArrayList<Project> projects = new ArrayList<>();
    private ArrayList<Project> selectProject = new ArrayList<>();
    private ProjectAdapter adapter;

    @Override
    protected int getContentView() {
        return R.layout.activity_choose_fix_project;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleView.setMyCallBack(this);

        type = getIntent().getIntExtra("type", 0);
        ArrayList<Project> list = getIntent().getParcelableArrayListExtra("data");
        ArrayList<Project> select = getIntent().getParcelableArrayListExtra("select");

        projects.addAll(list);
        selectProject.addAll(select);

        for (int i = 0; i < projects.size(); i++) {
            for (int j = 0; j < select.size(); j++) {
                if (projects.get(i).getId().equals(select.get(j).getId())) {
                    projects.get(i).setSelect(true);
                    break;
                }
            }
        }

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ProjectAdapter(R.layout.item_choose_fix_project, projects);
        dataRyc.setAdapter(adapter);

        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, null);
        TextView textView = emptyView.findViewById(R.id.content);
        textView.setText("暂无可选的维修项目哦!");
        adapter.setEmptyView(emptyView);

        adapter.setOnItemChildClickListener(this);

    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                finish();
                break;
            case 2:
                break;
            case 3:
                Intent intent = new Intent(mContext, NewProjectActivity.class);
                intent.putExtra("type", type);
                startActivityForResult(intent, 10);
                break;
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.checkbox:

                CheckBox checkBox = (CheckBox) view;
                if (checkBox.isChecked()) {
                    projects.get(position).setSelect(true);
                    adapter.notifyItemChanged(position);
                    selectProject.add(projects.get(position));
                } else {
                    projects.get(position).setSelect(false);
                    adapter.notifyItemChanged(position);
                    for (int i = 0; i < selectProject.size(); i++) {
                        if (projects.get(position).getId().equals(selectProject.get(i).getId())) {
                            selectProject.remove(i);
                        }
                    }
                }
                tvTip.setText("已选择" + selectProject.size() + "个项目");

                break;
        }
    }

    private void doRefresh() {
        AppApi.getFixProject(type, ChooseFixProjectActivity.this, 6, ChooseFixProjectActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            if (requestCode == 10) {
                doRefresh();
            }
        }
    }

    @Override
    public void handlerSuccessData(String result, int tag) {

        ProjectListBean projectListBean = JSON.parseObject(result, ProjectListBean.class);
        ArrayList<Project> projectArrayList = (ArrayList<Project>) projectListBean.getResultList();

        projects.clear();
        projects.addAll(projectArrayList);
        for (int i = 0; i < projects.size(); i++) {
            for (int j = 0; j < selectProject.size(); j++) {
                if (projects.get(i).getId().equals(selectProject.get(j).getId())) {
                    projects.get(i).setSelect(true);
                    break;
                }
            }
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    @OnClick({R.id.tv_tip, R.id.apply_form})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_tip:

                if (selectProject.size() > 0) {
                    ProjectNextDialogFragment projectNextDialogFragment = new ProjectNextDialogFragment();
                    projectNextDialogFragment.setMyCallBack(ChooseFixProjectActivity.this);
                    projectNextDialogFragment.show(getSupportFragmentManager(), "goods");
                } else {
                    SnackUtils.showError(mRootView, "您尚未选择任何项目！");
                }

                break;
            case R.id.apply_form:

                if (selectProject.size() > 0) {
                    Intent intent = new Intent();
                    intent.putParcelableArrayListExtra("data", selectProject);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    SnackUtils.showError(mRootView, "您尚未选择任何项目！");
                }
                break;
        }
    }

    public List<Project> getSelectProject(){
        return selectProject;
    }

    @Override
    public void doChange() {
        super.doChange();

        for (int i = 0; i < projects.size(); i++) {
            boolean isSelect = false;
            for (int j = 0; j < selectProject.size(); j++) {
                if (projects.get(i).getId().equals(selectProject.get(j).getId())) {
                    isSelect = true;
                    break;
                }
            }
            if(isSelect){
                projects.get(i).setSelect(true);
            }else {
                projects.get(i).setSelect(false);
            }
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    public void sure() {
        super.sure();
        if (selectProject.size() > 0) {
            Intent intent = new Intent();
            intent.putParcelableArrayListExtra("data", selectProject);
            setResult(RESULT_OK, intent);
            finish();
        } else {
            SnackUtils.showError(mRootView, "您尚未选择任何项目！");
        }
    }
}
